<?php
/********************
 *@param function:項目類別 model
 *@param datetime 2011-6-7
 *@param author abin
 ********************/
class categorys_mdl extends CI_model{
  //操作的表.
  const TAB_NAME='clickrcms_categorys';
  const TAB_DESC='clickrcms_categorys_description';

  function __construct(){
    parent::__construct();
    log_message('debug','categorys_mdl Model class Initialized');
  }

  /****
   *查詢所news,加分页
   *$data :查询参数.
   */
  public function get_categorys($data=array()){
    if(!empty($data)){
      extract($data);
      isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('n.sort_order ASC');
      isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
      isset($parent_id)?$this->db->where('n.parent_id',$parent_id):'';
      isset($on_parent_id)?$this->db->where('n.parent_id !=',$on_parent_id):'';
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      isset($title) && !empty($title)?$this->db->like('nd.title',$title):'';
      isset($unique_url) && !empty($unique_url)?$this->db->like('n.unique_url',$unique_url):'';
      isset($string_id) && !empty($string_id)?$this->db->where_in('n.id',$string_id):'';
      $this->db->select('n.*,nd.title,nd.introduction');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.category_id','LEFT');
      return $this->db->get();
    }
  }
  /****
   * 統計所有的categorys
   */
  public function totle_categorys($data=array()){
    if(!empty($data)){
      extract($data);
      $this->db->order_by('n.sort_order ASC');
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      isset($title) && !empty($title)?$this->db->like('nd.title',$title):'';
      isset($unique_url) && !empty($unique_url)?$this->db->like('n.unique_url',$unique_url):'';
      $this->db->select('n.*,nd.title');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.category_id','LEFT');
      return $this->db->count_all_results();
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  public function getCategories($data=array()) {
  
    if (!empty($data)){
      
      extract($data);
      
      $category_data = array();
      isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('n.sort_order ASC');
      isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
      isset($parent_id)?$this->db->where('n.parent_id',$parent_id):'';
      isset($on_parent_id)?$this->db->where('n.parent_id !=',$on_parent_id):'';
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      isset($title) && !empty($title)?$this->db->like('nd.title',$title):'';
      isset($unique_url) && !empty($unique_url)?$this->db->like('n.unique_url',$unique_url):'';
      $this->db->select('n.*,nd.*');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.category_id','LEFT');
      $query=$this->db->get();

      foreach ($query->result_array() as $result) {
        $filter_data=array('parent_id'=>$result['id'],'language_id'=>$language_id);
        $category_data[] = array(
          'id' => $result['id'],
          'title'       => $this->getPath($filter_data),
          'main_image'  => $result['main_image'],
          'main_icon'   => $result['main_icon'],
          'status'      => $result['status'],
          'unique_url'  => $result['unique_url'],
          'sort_order'  => $result['sort_order']
        );

       $category_data = array_merge($category_data, $this->getCategories($filter_data));
      }
      return $category_data;
    }
  }
  
  public function getPath($data=array()) {
    if(!empty($data)){
      extract($data);
      isset($parent_id)?$this->db->where('n.id',$parent_id):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      $this->db->select('n.parent_id,nd.title');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.category_id','LEFT');
      $query=$this->db->get();
      $title_name='';
      foreach ($query->result_array() as $result) {
        if($result['parent_id']){
          $filter_data=array('parent_id'=>$result['parent_id'],'language_id'=>$language_id);
          $title_name=$this->getPath($filter_data).' > '.$result['title'];
        }else{
          $title_name=$result['title'];
        }
      }
      return $title_name;
    }
  }
  /*****
   * 根據ID來刪除
   */
  public function delete_categorys($categorys_id=0){
    if(!empty($categorys_id)){
      $this->db->where("category_id",$categorys_id);
      $this->db->delete(self::TAB_DESC);
      $this->db->where("id",$categorys_id);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /****
   *根据ID来查询得到一个信息.
   *$id :要查詢的ID
   */
  public function get_categorys_by_id($id){
    $data=array();
    $this->db->where('id',$id);
    $get_result=$this->db->get(self::TAB_NAME);
    if($get_result->num_rows()>0)
    {
      $data=$get_result->row_array();
    }
    $get_result->free_result();
    return $data;
  }

  /***
   * 修改一個categorys
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_categorys($data=array(),$b_id=0){
    if(!empty($data) && !empty($b_id)){
      extract($data);
      $this->db->where('id',$b_id);
      $this->db->update(self::TAB_NAME,$general);
      //描述.
      if(isset($descriptions) && is_array($descriptions)){
        $this->db->where('category_id',$b_id);
        $this->db->delete(self::TAB_DESC);
        foreach ($descriptions as $language_id => $value) {
          $desc=array(
            'category_id'=>$b_id,
            'title'=>$value['title'],
            'introduction'=>$value['introduction'],
            'content'=>$value['content'],
            'language_id'=>$language_id
          );
          $this->db->insert(self::TAB_DESC,$desc);
        }
      }
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 新增一個 categorys
   */
  public function add_categorys($data=array()){
    if(!empty($data)){
      extract($data);
      $this->db->insert(self::TAB_NAME,$general);
      $category_id=$this->db->insert_id(); //獲取新增id
      if($category_id>0){
        //描述.
        if(isset($descriptions) && is_array($descriptions)){
          foreach ($descriptions as $language_id => $value) {
            $desc=array(
              'category_id'=>$category_id,
              'title'=>$value['title'],
              'introduction'=>$value['introduction'],
              'content'=>$value['content'],
              'language_id'=>$language_id
            );
            $this->db->insert(self::TAB_DESC,$desc);
          }
        }
        return ($this->db->affected_rows()>0)?true:false;
      } //end $category_id>0
    }
  }
  
  //根據id 獲取 多語言的descriptions
  public function get_descriptions($id=0){
	$lang = $this->settings['default_language'];  
    if((int)$id > 0){
        $this->db->where('category_id',$id);
    }
	$this->db->where('language_id',$lang);
    return $this->db->get(self::TAB_DESC);
  }
    //(前臺) 根據unique_url查詢.
  public function get_categorys_by_unique_url($data=array()){
    if(!empty($data)){
      extract($data);
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      isset($unique_url) && !empty($unique_url)?$this->db->where('n.unique_url',$unique_url):'';
      isset($id) && !empty($id)?$this->db->where('n.id',$id):'';
      $this->db->select('n.*,nd.*');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.category_id','LEFT');
      $query=$this->db->get();
      $return=array();
      if($query->num_rows()>0)
      {
        $return=$query->row_array();
      }
      $query->free_result();
      return $return;
    }
  }
  public function get_category_field(){
    $cates = $this->get_descriptions()->result_array();
    $data = array();
    if($cates){
        foreach ($cates as $key => $cat) {
          $data[$cat['category_id']] = $cat;
        }
    } 
    return $data;
  }
}